package com.csst.business.action;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.csst.business.model.BOnline;
import com.csst.business.model.TCompany;
import com.csst.business.model.TTrUser;
import com.csst.business.model.TWuser;
import com.csst.business.service.ICompanyService;
import com.csst.business.service.IOnlineService;
import com.csst.business.service.IUserService;
import com.csst.business.service.TestService;
import com.csst.business.util.PageData;
import com.csst.business.util.PaginationUtil;
import com.csst.popular.bean.User;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class OnlineAction extends ActionSupport{
	private IOnlineService onlineService;
	private ICompanyService companyService;
	private IUserService userService;
	private BOnline online;
	private List<BOnline> onlinelist;
	private TCompany company;
	private String orderNum;
	private String openid;
	//分页参数
	private PageData pageData;
	private int pagesize=3;
	private  int currentPage=1;
	public String testAction(){
		return "bbb";
	}
	
	public String  getUserById(){
		//TTrUser user = new TTrUser();
		return "success";
	}
	
	public void  saveOnline(){
		System.out.println(online.getOUsername() + "=="+online.getOHorseType());
		HttpServletRequest request = ServletActionContext.getRequest();
		TWuser tuser = (TWuser)request.getSession().getAttribute("tuser");
		if(tuser.getWOpenid()!=null){
			online.setOpenID(tuser.getWOpenid());
			online.setOPretime(new Date());
			online.setIsdel("1");
			online  = onlineService.saveOnline(online);
			//onlinelist =  onlineService.getOnlineList(user.getOpenid());
			try {
				ServletActionContext.getResponse().getWriter().print(true);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public String getWorderPage(){
		HttpServletRequest request = ServletActionContext.getRequest();
		TWuser tuser = (TWuser) request.getSession().getAttribute("tuser");
		if(tuser!=null){
			company = companyService.getCompanyData();
			List<BOnline> onlineList = onlineService.getOnlineList(tuser.getWOpenid());
			orderNum = onlineList.size()+"";
			return "onlinePagesB";
		}else{
			return "error";
		}
		
	}
	
	
	public String  getMyOnlineList(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String openId = (String) request.getSession().getAttribute("openId");
		if(openId==null){
			openId = (String)request.getParameter("openId");
			request.getSession().setAttribute("openId", openId);
		}
		TWuser tuser = userService.getUserByOpenId(openId);
		if(tuser.getWNickname()!=null){
			request.getSession().setAttribute("tuser", tuser);
			onlinelist =  onlineService.getOnlineList(openId);
			orderNum = onlinelist.size()+"";
			return "orderlist";
		}else{
			return "error";
		}
		
	}
	//查询全部订单列表
	public String getAllOnlineList(){
		int totle = onlineService.getOrderTotle();
		System.out.println(currentPage);
		pageData= PaginationUtil.getPageData(pagesize,totle, currentPage);
		onlinelist =  onlineService.getAllOnlineList(pageData.getStartNum(),pageData.getEndNum());
		return "orderlistB";
	}
	//查询全部订单列表
	public String getAllOnlineListByOpId(){
		openid = online.getOpenID();
		int totle = onlineService.getOrderTotle(online.getOpenID());
		System.out.println(currentPage);
		pageData= PaginationUtil.getPageData(pagesize,totle, currentPage);
		onlinelist =  onlineService.getAllOnlineList(online.getOpenID(),pageData.getStartNum(),pageData.getEndNum());
		return "memberorderlistB";
	}
	
	//删除订单
	public boolean deleteOrderAjax(String id){
		return onlineService.deleteOrderById(id);
	}

	public IOnlineService getOnlineService() {
		return onlineService;
	}

	public void setOnlineService(IOnlineService onlineService) {
		this.onlineService = onlineService;
	}

	public BOnline getOnline() {
		return online;
	}

	public void setOnline(BOnline online) {
		this.online = online;
	}

	public List<BOnline> getOnlinelist() {
		return onlinelist;
	}

	public void setOnlinelist(List<BOnline> onlinelist) {
		this.onlinelist = onlinelist;
	}

	public PageData getPageData() {
		return pageData;
	}

	public void setPageData(PageData pageData) {
		this.pageData = pageData;
	}

	public int getPagesize() {
		return pagesize;
	}

	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public ICompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(ICompanyService companyService) {
		this.companyService = companyService;
	}

	public TCompany getCompany() {
		return company;
	}

	public void setCompany(TCompany company) {
		this.company = company;
	}

	public String getOrderNum() {
		return orderNum;
	}

	public void setOrderNum(String orderNum) {
		this.orderNum = orderNum;
	}

	public String getOpenid() {
		return openid;
	}

	public void setOpenid(String openid) {
		this.openid = openid;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	
}
